package test;
//  总测试

import sun.awt.AWTAccessor;
import t1.MethodCalculate;
import t1.SFE_s;
import t1.WordAndWordSum;
import t1.fileIn;
import t1.codingMethod;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class mainT3 {
    public static void main(String[] args) throws IOException {

        System.out.println("========================== part 1  =======================");

        //        调用getStringInFile  返回AllWord
//        需要传入文本 的文件对象
        File parentFile = new File("E:\\");
        System.out.println("输入文件路径");
        long t1_1 = System.currentTimeMillis();
//         符号测试  代码 修改直接 将该行注释  将下一行注释取消即可
//        String childPath = "test2.txt";
        String childPath = new Scanner(System.in).next();
        File inputFile = new File(parentFile, childPath);
        long t1 = System.currentTimeMillis();
//        File inputFile = new File("E:\\123test.txt");
        fileIn fileIn = new fileIn();
        ArrayList<char[]> AllWord = fileIn.getStringInFile(inputFile);
        System.out.println("=======AllWord=======");
        for (char[] chars : AllWord) {
            System.out.println(chars);
        }
//        调用getWordSum 返回WordAndWordSum 并对其进行拆分
        WordAndWordSum wordAndWordSum = fileIn.getWordSum(AllWord);
        System.out.println("=======wordAndWordSum=======");
        System.out.println(wordAndWordSum);
//        对wordAndWordSum 进行拆分 得到 word_sum  字符出现的个数
        ArrayList<Integer> word_sum = wordAndWordSum.getWord_sum();
        System.out.println("=======word_sum=======");
        System.out.println(word_sum);
//        对wordAndWordSum 进行拆分 得到 编码数组 word_array
        ArrayList<Character> word_array = wordAndWordSum.getWordArray();
        System.out.println("=======word_array=======");
        System.out.println(word_array);
//        对wordAndWordSum 进行拆分 得到 文本字符总数
        int sum = wordAndWordSum.getSum();
        System.out.println("=====sum=======");
        System.out.println(sum);

//        分别进行打印 两个数组中的元素个数
        System.out.println("=====word_array.size()====");
        System.out.println(word_array.size());
        System.out.println("=====word_sum.size()====");
        System.out.println(word_sum.size());
//        for (int i = 0; i < word_sum.size(); i++) {
//            System.out.println(word_array.get(i)+"=="+word_sum.get(i));
//        }

//        System.out.println("========word_array + index=========");
//        for (int i = 0; i < word_array.size(); i++) {
//            System.out.print(word_array.get(i)+"_"+i);
//        }
        System.out.println("\n");
        System.out.println("====word_array====");
        System.out.println(word_array);
        System.out.println("=======word_sum======");
        System.out.println(word_sum);
        System.out.println("=====sum====");
        System.out.println(sum);
        System.out.println("=========");
        long t1_2 = System.currentTimeMillis();
        System.out.println("读取文件 和计算 码字花费了："+(t1_2-t1_1));


        System.out.println("======================= part 2=============================");
//      计算字符编码阶段
        long t2_1 = System.currentTimeMillis();
        MethodCalculate method = new MethodCalculate();
        //        1 调用CalculatePx返回 px_array

        ArrayList<Double> px_array = method.CalculatePx(sum, word_sum);
        System.out.println("==== px 输出 =====");
        System.out.println(px_array);

//         2 调用CalculateFx 返回fx_array
        ArrayList<Double> fx_array = method.CalculateFx(px_array);
        System.out.println("===========fx 输出=============");
        System.out.println(fx_array);

//        调用CalculateFabx 返回fabx_array
        ArrayList<Double> fbax_array = method.CalculateFabx(px_array, fx_array);

//          调用CalculateLx 返回lx_array
        ArrayList<Integer> lx_array = method.CalculateLx(px_array);

//        调用transform 返回trans_array
        ArrayList<String> trans_array = method.Transform(fbax_array, lx_array);

//        调用CalculateCode 返回Code_array
        ArrayList<String> Code_array = method.CalculateCode(lx_array, trans_array);
//
//        创建存储码字信息的arraylist
        ArrayList<SFE_s> sfe_s = new ArrayList<>();
        method.Assignment(sfe_s,word_array,px_array,fx_array,fbax_array,lx_array,Code_array,trans_array);

        long t2_2 = System.currentTimeMillis();
        System.out.println("计算码字信息 花费了:"+(t2_2 - t2_1));

        System.out.println("======================= part 3=============================");
//       文件编译 阶段和 输出文件阶段
        long t3_1 = System.currentTimeMillis();
        codingMethod codingMethod = new codingMethod();


//        返回 编码好的 字符串数组
        ArrayList<String> code_string = codingMethod.coding(AllWord, word_array, Code_array);
        System.out.println("=========测试输出 编码好的 字符串============");
        System.out.println(code_string);
        long t2_2_1 = System.currentTimeMillis();
//      将编码好的字符串数组 输出至指定文件
        System.out.println("请输入 编译后文件名");

        String outChildPath = new Scanner(System.in).next();
//         符号测试　将上一行注释　下一行　取消注释即可
//        String outChildPath = "fuhao.txt";
        long t2_2_2 = System.currentTimeMillis();
        File outFile = new File(parentFile, outChildPath);
//        File codeTable = new File("E:\\codeTable.txt");tes
//         实验符号　输出的编码表名称
        File codeTable = new File("E:\\codeTable2.txt");

//        File outFile = new File("E:\\outTest.txt");
        codingMethod.writeCodingFile(outFile,codeTable,code_string,sfe_s);
        long t3_2 = System.currentTimeMillis();
        System.out.println("文件编译 阶段和 输出文件阶段 花费了:"+(t3_2-t3_1));
        long t2_2_3 = System.currentTimeMillis();


        System.out.println("测试总共花费了 :"+(t2_2_3-t2_2_2+t2_2_1-t1));

    }
}
